'''
n = int(input())
a,b = map(int, input().split())
arr = list(map(int, input().split()))
'''
cases = int(input())
for case in range(cases):
n, x = map(int, input().split())
nums = list(map(int, input().split()))
diff = 0
for i in range(n-1):
diff += abs(nums[i] - nums[i+1])
min_val = min(nums)
max_val = max(nums)
if x > max_val:
diff += min((x-max_val)*2, x - max(nums[0], nums[-1]))
if min_val > 1:
diff += min((min_val-1)*2, min(nums[0], nums[-1])-1)
print(diff)
/*
key : if elements between max of array and min of array, we dont need to
consider
1) element less than min of array, find their position
2)element greater than max of array, find their position
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define MOD 998244353
#define inf (1LL<<60)
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define uint unsigned long long
uint power(int x, int y, int p = MOD)
{
unsigned long long res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
uint modInverse(int n, int p = MOD)// using fermats little thm. [p needs to be prime which is mostly the case as mod value generally is 1e9+7]
{
return power(n, p - 2, p);
}
// =========================================Used to calculate nCr of higher values ===================================
uint nCr(int n, int r, int p = MOD) // faster calculation..
{
if (n < r)
return 0;
if (r == 0)
return 1;
vector<int> fac(n+1,0);
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
void init_code()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/*
think before you start to code, atleast test your approach on all tc
C - bs/dp/nCr/bitmasking/constructive(see ex)/brute/greedy/graphs
*/
const int N = 1e5;
int main()
{
fast_io;
init_code();
int tt;
cin >> tt;
while(tt--)
{
ll n, x;
cin >> n >> x;
vector<ll> v(n);
ll ans = 0;
for(int i = 0; i<n; i++)
{
cin >> v[i];
if(i != 0)
ans += abs(v[i] - v[i-1]);
}
ll least = *min_element(v.begin(), v.end());
ll greatest = *max_element(v.begin(), v.end());
if(1 < least)
least = 1;
if(x > greatest)
greatest = x;
ll cnt1 = min(abs(least - v[0]), abs(least - v[n-1])), cnt2 = min(abs(greatest - v[0]), abs(greatest - v[n-1]));
for(int i = 1; i<n; i++)
{
cnt1 = min(cnt1, abs(least - v[i]) + abs(least - v[i-1]) - abs(v[i] - v[i-1]));
}
for(int i = 1; i<n; i++)
{
cnt2 = min(cnt2, abs(greatest - v[i]) + abs(greatest - v[i-1]) - abs(v[i] - v[i-1]));
}
cout << ans + cnt1 + cnt2 << endl;
}
return 0;
}
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |
1822. Sign of the Product of an Array | 1464. Maximum Product of Two Elements in an Array |
1323. Maximum 69 Number | 832. Flipping an Image |
1295. Find Numbers with Even Number of Digits | 1704. Determine if String Halves Are Alike |
1732. Find the Highest Altitude | 709. To Lower Case |
1688. Count of Matches in Tournament | 1684. Count the Number of Consistent Strings |
1588. Sum of All Odd Length Subarrays | 1662. Check If Two String Arrays are Equivalent |
1832. Check if the Sentence Is Pangram | 1678. Goal Parser Interpretation |
1389. Create Target Array in the Given Order | 1313. Decompress Run-Length Encoded List |
1281. Subtract the Product and Sum of Digits of an Integer | 1342. Number of Steps to Reduce a Number to Zero |
1528. Shuffle String | 1365. How Many Numbers Are Smaller Than the Current Number |
771. Jewels and Stones | 1512. Number of Good Pairs |
672. Richest Customer Wealth | 1470. Shuffle the Array |
1431. Kids With the Greatest Number of Candies | 1480. Running Sum of 1d Array |
682. Baseball Game | 496. Next Greater Element I |